#include "lgaparamdialog.h"
#include "ui_lgaparamdialog.h"
#include "lgapackagemanager.h"
#include <QSettings>
LgaParamDialog::LgaParamDialog(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::LgaParamDialog)
{
	ui->setupUi(this);
	setWindowFlags(Qt::WindowCloseButtonHint | Qt::Dialog);	
	LgaPackageManager::INSPECT_PARAM param = LgaPackageManager::getInstance()->GetParamFromFile();
	QString appName = QCoreApplication::applicationName();
	QSettings app(appName, "Debug");
	bool bEnable = app.value("TestItemEnable").toBool();
	if (bEnable)
	{
		ui->checkBox_cap->setEnabled(true);
		ui->checkBox_pad->setEnabled(true);
		ui->checkBox_substrate->setEnabled(true);
	}
	QRegExp rx("^([0]|[1-9][0-9]{0,5})(?:\\.\\d{1,4})?$|(^\\t?$)");
	QRegExpValidator *pReg = new QRegExpValidator(rx, this);
	ui->lineEdit->setValidator(pReg);
	ui->lineEditWidth->setValidator(pReg);
	ui->lineEditHeight->setValidator(pReg);
;
	//InspectParamManager::INSPECT_PARAM param = InspectParamManager::getInstance()->GetDefaultParam();
	ui->checkBox_cap->setChecked(param.bCapDamaged);
	ui->checkBox_pad->setChecked(param.bPadDamaged);
	ui->checkBox_substrate->setChecked(param.bChipping);
	//ui->checkBox_Bypass->setChecked(param.bBypass);
	ui->checkBox_SaveFail->setChecked(param.bOnlySaveFail);
	//ui->spinBox->setValue(param.miniChippingArea);
	ui->lineEdit->setText(QString::number(param.miniChippingArea));
	ui->lineEditWidth->setText(QString::number(param.miniChippingWidth));
	ui->lineEditHeight->setText(QString::number(param.miniChippingHeight));
	ui->spinBox_2->setValue(param.saveDays);
	ui->spinBox_3->setValue(param.miniAreaRatio);
	ui->spinBox_4->setValue(param.miniCountOfLarge);
	ui->horizontalSlider->setValue(param.discolorValue);
	
	ui->checkBox_discolor->setText(QString("Discolor value: %1").arg(param.discolorValue));
	ui->checkBox_discolor->setChecked(param.bDisColor);
	//ui->checkBox_Seperate->setChecked(param.bSeperateFolder);
	//ui->spinBox_6->setValue(param.iCountForSeperateFolder);

	ui->spinBoxCapRatio->setValue(param.capMiniAreaRatio);


	connect(ui->pushButton, &QPushButton::clicked, [=]() {
		LgaPackageManager::INSPECT_PARAM param;
		param.bCapDamaged = ui->checkBox_cap->isChecked();
		param.bPadDamaged = ui->checkBox_pad->isChecked();
		param.bChipping = ui->checkBox_substrate->isChecked();
		param.bOnlySaveFail = ui->checkBox_SaveFail->isChecked();
	//	param.miniChippingArea = ui->spinBox->value();
		param.miniChippingArea = ui->lineEdit->text().toDouble();
		param.miniChippingWidth = ui->lineEditWidth->text().toDouble();
		param.miniChippingHeight = ui->lineEditHeight->text().toDouble();
      //  param.bBypass = ui->checkBox_Bypass->isChecked();
		param.saveDays = ui->spinBox_2->value();
		param.miniAreaRatio = ui->spinBox_3->value();
		param.miniCountOfLarge = ui->spinBox_4->value();
		param.discolorValue = ui->horizontalSlider->value();
		param.bDisColor = ui->checkBox_discolor->isChecked();
	//	param.bSeperateFolder = ui->checkBox_Seperate->isChecked();
	//	param.iCountForSeperateFolder = ui->spinBox_6->value();

		param.capMiniAreaRatio = ui->spinBoxCapRatio->value();

		LgaPackageManager::getInstance()->SaveParamToFile(param);
	
		QDialog::accept();
	});

	connect(ui->horizontalSlider, &QSlider::valueChanged, [=](int value)
	{
		ui->checkBox_discolor->setText(QString("Discolor value: %1").arg(value));
	});

	connect(ui->pushButton_2, &QPushButton::clicked, [=]() {
		QDialog::reject();
	});
}

LgaParamDialog::~LgaParamDialog()
{
    delete ui;
}
